package io.zw.modules.xinpai.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.zw.common.validator.ValidatorUtils;
import io.zw.common.validator.group.AddGroup;
import io.zw.common.validator.group.ProjectGroup;
import io.zw.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.zw.modules.xinpai.entity.ProjectEntity;
import io.zw.modules.xinpai.service.ProjectService;
import io.zw.common.utils.PageUtils;
import io.zw.common.utils.R;



/**
 * 
 *
 * @author zhuwei
 * @email 121203654@qq.com
 * @date 2018-10-10 13:15:04
 */
@RestController
@RequestMapping("xinpai/project")
public class ProjectController extends AbstractController {
    @Autowired
    private ProjectService projectService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("xinpai:project:list")
    public R list(@RequestParam Map<String, Object> params){
        //PageUtils page = projectService.queryPage(params);
        List<ProjectEntity> list=projectService.selectList(new EntityWrapper<ProjectEntity>().eq("parent_id",params.get("parentId")).eq("del_flag",0).eq("type",params.get("type")).eq("firm_id",getDeptId()));
        //List<ProjectEntity> list=projectService.selectByMap(params);
        return R.ok().put("list",list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{pId}")
    @RequiresPermissions("xinpai:project:info")
    public R info(@PathVariable("pId") Long pId){
        ProjectEntity project = projectService.selectById(pId);

        return R.ok().put("project", project);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("xinpai:project:save")
    public R save(@RequestBody ProjectEntity project){
        project.setDelFlag(0);
        if(project.getParentId().intValue()>0){
            ValidatorUtils.validateEntity(project, ProjectGroup.class);
        }else {
            ValidatorUtils.validateEntity(project, AddGroup.class);
        }
        project.setFirmId(getDeptId());
        projectService.insert(project);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("xinpai:project:update")
    public R update(@RequestBody ProjectEntity project){
        ValidatorUtils.validateEntity(project);
        projectService.updateAllColumnById(project);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("xinpai:project:delete")
    public R delete(@RequestBody ProjectEntity project){
        if(project.getParentId()==0){
            if(projectService.selectList(new EntityWrapper<ProjectEntity>().eq("parent_id",project.getPId()).eq("del_flag",0)).size()>0){
                return R.error("请先删除该项目下的区域");
            }
        }
        projectService.deleteById(project.getPId());
        return R.ok();
    }

}
